#pragma once
#include <unistd.h>
#include "Data.hpp"

extern cloud::DataManager *_data;
namespace cloud
{
    class HotManager
    {
    private:
        std::string _back_str;
        std::string _pack_str;
        std::string _pack_suffix;
        int _hot_time; // 热点判断时间
    public:
        HotManager()
        {
            Config *cf = Config::GetInstance();
            _back_str = cf->GetBackDir();
            _pack_str = cf->GetPackDir();
            _pack_suffix = cf->GetPackFileSuffix();
            _hot_time = cf->GetHotTime();
            FileUtil fuback(_back_str);
            fuback.CreateDirectory();
            FileUtil fupack(_pack_str);
            fupack.CreateDirectory();
        }
        bool HotJudge(std::string path)
        {
            FileUtil fu(path);
            time_t atime = fu.LastATime();
            time_t ctime = time(nullptr);
            if (ctime - atime > _hot_time)
            {
                return true;
            }
            return false;
        }
        bool RunModule()
        {
            while (true)
            {
                FileUtil fu(_back_str);
                std::vector<std::string> arry;
                fu.GetDirectory(&arry);
                for (auto &e : arry)
                {
                    if (HotJudge(e) == false)
                        continue;
                    BackupInfo bi;
                    if (_data->GetOneRealpath(e, &bi) == false)
                    {
                        bi.NewBackupInfo(e);
                    }
                    FileUtil tmp(e);
                    tmp.Compress(bi.pack_path);
                    bi.pack_flag = true;
                    _data->Update(bi);
                    tmp.Remove();
                }
                usleep(1000);
            }
            return true;
        }
    };
}